package jude.java.money.test.init;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.framework.test.SpringTestCase;
import jude.java.money.beans.FtpSite;
import jude.java.money.service.GlobalService;
import jude.java.money.service.HeiService;
import jude.java.money.spring.Services;
import jude.java.util.WebUtils;
import jude.java.util.parse.ExcelParser;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class InitBlackLink extends SpringTestCase {
	
	public void testInitFtpSites() throws Exception {
		insertFtpSites4();
		insertFtpSites5();
	}

	public static void insertFtpSites(String excelPath) throws IOException {
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelPath));
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		HSSFSheet sheet = wb.getSheetAt(0);
		int count = sheet.getLastRowNum();
		GlobalService service = Services.getGlobalService();
		System.out.println(count);
//		String last = null;
		for (int i=0; i<count; i++) {
			String cellName = "A" + (i + 1);
			String value = ExcelParser.getCellValue(sheet, cellName);
			if (StringUtils.isNotEmpty(value)) {
				String[] strs = value.split(",");
				if (strs.length == 0)
					continue;
//				if (strs[0].equals(last))	//过滤连续重复的
//					continue;
//				last = strs[0];
				FtpSite ftp = service.get(FtpSite.class, "host", strs[0]);
				if (ftp == null)
					ftp = new FtpSite();
				switch (strs.length) {
				case 1:
					ftp.init(strs[0], "Anonymous", "");
					break;
				case 2:
					ftp.init(strs[0], strs[1], "");
					break;
				case 3:
				default:
					ftp.init(strs[0], strs[1], strs[2]);
					break;
				}
				service.save(ftp);
				System.out.println(i + " " + ftp.getHost());
			}
		}
	}
	
	public static void insertFtpSites2(String excelPath, int... sheets) throws IOException {
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelPath));
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		for (int j = 0; j < sheets.length; j++) {
			HSSFSheet sheet = wb.getSheetAt(sheets[j]);
			int count = sheet.getLastRowNum();
			GlobalService service = Services.getGlobalService();
			System.out.println(count);
//			String last = null;
			List<String> domains = new ArrayList<String>();
			for (int i=0; i<=count; i++) {
				String cellName = "A" + (i + 1);
				String value = ExcelParser.getCellValue(sheet, cellName);
				value = value == null ? null : value.trim();
				if (StringUtils.isNotEmpty(value)) {
					String[] strs2 = value.split(",");
					if (strs2.length == 0)
						continue;
					
					String[] strs = WebUtils.parseFtpPath(strs2[0]);
					if (strs == null || StringUtils.isEmpty(strs[0]))
						continue;
//					if (strs[0].equals(last))	//过滤连续重复的
//						continue;
//					last = strs[0];
					domains.clear();
					domains.add(strs[0]);
					if (strs2.length > 1) {
						String[] ds = StringUtils.split(strs2[1]);
						for (int k = 0; k < ds.length; k++) {
							domains.add(ds[k]);
						}
					}
					for (String domain : domains) {
						FtpSite ftp = service.get(FtpSite.class, "host", domain);
						if (ftp != null)
							continue;
						ftp = new FtpSite();
						
						switch (strs.length) {
						case 1:
							ftp.init(domain, "Anonymous", "");
							break;
						case 2:
							ftp.init(domain, strs[1], "");
							break;
						case 3:
						default:
							ftp.init(domain, strs[1], strs[2]);
							break;
						}
						service.save(ftp);
						System.out.println(i + " " + ftp.getHost());
					}
				}
			}
		}
	}
	
	public static void insertFtpSites3(String excelPath) throws IOException {
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelPath));
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		HSSFSheet sheet = wb.getSheetAt(0);
		int count = sheet.getLastRowNum();
		GlobalService service = Services.getGlobalService();
		System.out.println(count);
		for (int i=0; i<=count; i++) {
			String host = ExcelParser.getCellValue(sheet, "A" + (i + 1));
			if (StringUtils.isBlank(host))
				continue;
			FtpSite ftp = service.get(FtpSite.class, "host", host);
			if (ftp == null) {
				ftp = new FtpSite();
			}
			String usr = ExcelParser.getCellValue(sheet, "B" + (i + 1));
			String pwd = ExcelParser.getCellValue(sheet, "C" + (i + 1));
//			ftp = new FtpSite();
			ftp.init(host, usr, pwd);
			ftp.setLoginTimes(1);
			ftp.setLogined(1);
			service.save(ftp);
			System.out.println(i + 1 + " " + host);
		}
	}
	
	public static void insertFtpSites4() throws IOException {
		File folder = new File("M:\\Users\\Jude\\Desktop\\QQRecv\\弱口令资源\\");
		File[] files = folder.listFiles(new FileFilter() {
			@Override
			public boolean accept(File file) {
				return file.isFile() && file.getName().toLowerCase().endsWith(".txt");
			}
		});
		int all = 0;
		HeiService service = Services.getHeiService();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			int total = 0;
			int exist = 0;
			System.out.println("--------------------------" + f.getName());
			//www.qzqz.cn 用户名：qzqz 密码：qzqz-成功
			Pattern p = Pattern.compile("^([\\w\\.\\-]*)\\s*用户名：?\\s*([\\w\\-]*)\\s*密码：?\\s*([\\w\\-]*)-成功");
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "GBK"));
			String line;
			while((line = reader.readLine()) != null) {
				line = line.trim();
				if (line.length() == 0)
					continue;
				Matcher m = p.matcher(line);
				if (m.find()) {
					String host = m.group(1).toLowerCase();
					String usr = m.group(2);
					String pwd = m.group(3);
					FtpSite site = new FtpSite(host, usr, pwd);
					try {
						service.insert(site);
						total ++;
					} catch (Exception e) {
						exist ++;
					}
				} else {
					System.out.println("\t\t" + line);
				}
			}
			all += total;
			System.out.println("++++++++++++++++++++++++++" + total + " " + exist + "\t" + f.getName());
		}
		System.out.println(all);
	}
	
	public static void insertFtpSites5() throws IOException {
		File[] files = new File[] {
//				new File("M:\\Users\\Jude\\Desktop\\QQRecv\\弱口令资源\\8.30综合.txt"),
				new File("M:\\Users\\Jude\\Desktop\\QQRecv\\弱口令资源\\w20100629.0.txt"),
		};
		HeiService service = Services.getHeiService();
		int all = 0;
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			int total = 0;
			int exist = 0;
			System.out.println("--------------------------" + f.getName());
			//www.wzyouth.com,admin,admin
			Pattern p = Pattern.compile("(.+),(.+),(.+)");
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "GBK"));
			String line;
			while((line = reader.readLine()) != null) {
				line = line.trim();
				if (line.length() == 0)
					continue;
				Matcher m = p.matcher(line);
				if (m.find()) {
					String host = m.group(1).toLowerCase();
					String usr = m.group(2);
					String pwd = m.group(3);
					FtpSite site = new FtpSite(host, usr, pwd);
					try {
						service.insert(site);
						total ++;
					} catch (Exception e) {
						exist ++;
					}
				} else {
					System.out.println("\t\t" + line);
				}
			}
			all += total;
			System.out.println("++++++++++++++++++++++++++" + total + " " + exist + "\t" + f.getName());
		}
		System.out.println(all);
	}
}
